<template>
  <div class="home">
    <!-- 顶部状态栏 -->
    <el-header class="topbar">
      <div class="topbar-content">
        <div class="user-info">
          <span>{{ username }}</span>
          <el-button type="text" @click="handleLogout" class="logout-button">退出</el-button>
        </div>
        <!-- 搜索框 -->
        <div class="search-container">
          <input v-model="searchQuery" placeholder="搜索书籍..." class="search-input" />
          <button @click="performSearch" class="search-button">搜索</button>
        </div>
      </div>
    </el-header>

    <!-- 主内容 -->
    <div class="main-content">
      <!-- 借阅记录展示 -->
      <div class="borrow-records-container">
        <div class="card">
          <div class="card-header">
            <h3>借阅记录</h3>
          </div>
          <div class="table-container">
            <table class="records-table">
              <thead>
              <tr>
                <th>用户信息</th>
                <th>书籍</th>
                <th>借阅日期</th>
                <th>归还日期</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <tr v-for="record in borrowRecords" :key="record.id">
                <td>{{ record.username }}</td>
                <td>{{ record.bookName }}</td>
                <td>{{ formatDate(record.borrowDate) }}</td>
                <td>{{ formatDate(record.returnDate) }}</td>
                <td>
                  <button @click="viewUserInfo(record.username)" class="info-button">查看用户</button>
                  <button @click="deleteRecord(record.id)" class="delete-button">删除</button>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>

      <!-- 用户信息展示弹出框 -->
      <div v-if="userInfoDialogVisible" class="dialog-overlay">
        <div class="dialog">
          <div class="dialog-header">
            <h3>{{ selectedUsername }} 的借阅记录</h3>
            <button @click="userInfoDialogVisible = false" class="close-button">×</button>
          </div>
          <div class="dialog-body">
            <div v-if="userRecords.length === 0">没有找到借阅记录。</div>
            <div v-else>
              <table class="records-table">
                <thead>
                <tr>
                  <th>书籍</th>
                  <th>借阅日期</th>
                  <th>归还日期</th>
                  <th>状态</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="record in userRecords" :key="record.id">
                  <td>{{ record.bookName }}</td>
                  <td>{{ formatDate(record.borrowDate) }}</td>
                  <td>{{ formatDate(record.returnDate) }}</td>
                  <td>{{ record.status }}</td>
                </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>

      <!-- 搜索结果展示 -->
      <div v-if="dialogVisible" class="dialog-overlay">
        <div class="dialog">
          <div class="dialog-header">
            <h3>搜索结果</h3>
            <button @click="dialogVisible = false" class="close-button">×</button>
          </div>
          <div class="dialog-body">
            <div v-if="books.length === 0">没有找到图书信息。</div>
            <div v-else>
              <div class="books-container">
                <div v-for="book in books.slice(0, 3)" :key="book.id" class="book-card">
                  <h3>{{ book.bookName }}</h3>
                  <p><strong>作者:</strong> {{ book.author }}</p>
                  <p><strong>出版社:</strong> {{ book.publishingHouse }}</p>
                  <p><strong>出版日期:</strong> {{ formatDate(book.publishDate) }}</p>
                  <p><strong>页数:</strong> {{ book.pages }}</p>
                  <p><strong>ISBN:</strong> {{ book.ISBN }}</p>
                  <p><strong>价格:</strong> {{ book.price }}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      borrowRecords: [],
      userRecords: [],
      selectedUsername: '',
      userInfoDialogVisible: false,
      searchQuery: '',
      dialogVisible: false,
      books: []
    };
  },
  created() {
    this.fetchBorrowRecords();
  },
  methods: {
    async fetchBorrowRecords() {
      try {
        const response = await axios.get('http://localhost:8080/borrowRecords?action=allRecords');
        this.borrowRecords = response.data;
      } catch (error) {
        console.error('Error fetching borrow records:', error);
      }
    },
    async deleteRecord(recordId) {
      try {
        await axios.delete(`http://localhost:8080/borrowRecords/${recordId}`);
        this.fetchBorrowRecords(); // Refresh the records after deletion
      } catch (error) {
        console.error('Error deleting record:', error);
      }
    },
    async viewUserInfo(username) {
      try {
        const response = await axios.get(`http://localhost:8080/borrowRecords?action=userRecords&username=${username}`);
        this.userRecords = response.data;
        this.selectedUsername = username;
        this.userInfoDialogVisible = true;
      } catch (error) {
        console.error('Error fetching user records:', error);
      }
    },
    formatDate(date) {
      const d = new Date(date);
      return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
    },
    async performSearch() {
      try {
        const response = await axios.get(`http://localhost:8080/borrowRecords?action=searchBooks&bookName=${this.searchQuery}`);
        this.books = response.data;
        this.dialogVisible = true;
      } catch (error) {
        console.error('Error searching books:', error);
      }
    },
    handleLogout() {
      // Handle logout logic here
    }
  }
};
</script>

<style scoped>
.home {
  text-align: center;
}

.topbar {
  position: fixed;
  top: 0;
  left: 300px; /* 适应左侧导航栏的宽度 */
  right: 0;
  height: 60px;
  background-color: #ffffff;
  color: #100e0e;
  z-index: 1000;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.topbar-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-info span {
  margin-left: 10px;
}

.logout-button {
  color: #ff0000;
}

.search-container {
  display: flex;
  align-items: center;
}

.search-input {
  width: 600px;
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #ccc;
}

.search-button {
  padding: 10px 20px;
  margin-left: 10px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
}

.main-content {
  margin-top: 80px; /* 确保主内容不被固定的状态栏遮挡 */
  padding-top: 20px; /* 确保内容不被状态栏遮挡 */
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog {
  background: white;
  padding: 20px;
  border-radius: 8px;
  width: 80%;
  max-width: 600px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
}

.dialog-body {
  margin-top: 20px;
}

.records-table {
  width: 100%;
  border-collapse: collapse;
}

.records-table th,
.records-table td {
  border: 1px solid #ebeef5;
  padding: 10px;
  text-align: center;
}

.records-table th {
  background-color: #f5f5f5;
}
</style>
